﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.DO;
using VINETLIFE.BL;
using System.Data;

namespace VINETLIFE
{
    public partial class TestShop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Id"] != null)
                {
                    
                }
                if (Session["shop"] == null)
                {
                    Session["shop"] = new MyShopCart();
                }
                ShowData();
            }
            
        }

        private void ShowData()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Id");
            table.Columns.Add("Name");
            table.Columns.Add("Cost");
            table.Columns.Add("Image");
            table.Columns.Add("Quantity");
            table.Columns.Add("TotalCost");
            if (Session["shop"] != null)
            {
                foreach (var item in ((MyShopCart)Session["shop"]).ListProduct)
                {

                    DataRow row = table.NewRow();
                    row["Id"] = item.Id;
                    row["Name"] = item.Name;
                    if (item.Cost == null)
                    {
                        String.Format("{0:0,0}", 0);
                    }
                    else
                    {
                        row["Cost"] = String.Format("{0:0,0}", item.Cost);
                    }
                    row["Image"] = item.Image;
                    row["Quantity"] = item.Quantity;
                    row["TotalCost"] = String.Format("{0:0,0}", item.Quantity * item.Cost);

                    table.Rows.Add(row);
                }

                rpProduct.DataSource = table;
                rpProduct.DataBind();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            AddItem();
        }

        cmsProductBL data = new cmsProductBL();
        private void AddItem()
        {
            cmsProductDO itemDO = new cmsProductDO();
            //itemDO.ProductID = Convert.ToInt32(Request.QueryString["Id"]);
            itemDO.ProductID = 15;
            cmsProductDO product = data.Select(itemDO);
            if (product != null)
            {
                ProductShop item = new ProductShop();
                item.Id = product.ProductID;
                item.Image = product.ProductImage;
                item.Name = product.ProductTitle;
                if (product.Price == null || product.Price.ToString() == "")
                    item.Cost = 0;
                else
                    item.Cost = (decimal)product.Price;
                item.Quantity = 1;

                ((MyShopCart)Session["shop"]).Add(item);
                
            }
            Response.Redirect(this.Request.Url.ToString());
           // ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>$(document).ready(fuction(){ $('[id*=btnPopup]').live('click', function () { $('#dialog').dialog({title: 'jQuery Dialog Popup',buttons: { Close: function () {$(this).dialog('close'); }} }); return false;}); });</script>", false);
        }
    }
}